Skip to content

Fix scientific and compact formatting of zero - #682

Merged
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-gfh5qny5ce
Nov 25, 2026
Merged

nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-gfh5qny5ce

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

format_c turns %e with 0.0 into NaN.000000e-2147483648 instead of 0.000000e+00. For the same zero argument, %g and %G panic in debug builds and emit NaN. in release builds. The refcount printf, fprintf and snprintf paths now format scientific (%e/%E) and compact (%g/%G) zeros directly, avoiding logarithmic normalization.

The shared helper preserves negative zero and applies precision, signs and field width. Scientific zero uses e+00 or E+00. Compact zero uses fixed form: without #, it emits 0 regardless of precision; %#.4g emits 0.000, while %#.0g keeps the decimal point as 0.. A + sign takes precedence over a space sign, and left alignment overrides zero padding.

The C fixture in tests/unit/printf_zero.c compares both translation models with native output. It covers these cases, including unpadded and zero-padded space signs, widths shorter than the value, and multiple conversions in one format.

Nonzero, subnormal and non-finite values, and %f/%F, retain the existing formatter; this is not a general normalization or rounding repair.

Testing: Lit's zero-formatting, snprintf and printf fixtures passed: 3 passed, 0 failed, 0 skipped. Cargo's existing formatting tests passed: 6 passed, 0 failed, 0 ignored. Rustfmt, clang-format and Clippy checks passed.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@nunoplopes

Copy link
Copy Markdown
Contributor

@perfloop-agent don't add cargo tests. We want only C tests so we can ensure we give the same result as C/C++.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

The added Rust tests have been replaced with tests/unit/printf_zero.c, whose output is compared with native C in both translation models. The formatting code is unchanged, and the description now lists the C test results.

@nunoplopes
nunoplopes merged commit b52465f into Cpp2Rust:master Nov 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants